package com.yzd.crm.service.impl;

import com.yzd.crm.dao.LinkManDao;
import com.yzd.crm.domain.LinkMan;
import com.yzd.crm.domain.PageBean;
import com.yzd.crm.service.LinkManService;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/4/5 17:36
 * @Description:
 */
@Service("linkManService")
public class LinkManServiceImpl implements LinkManService {

    @Autowired
    private LinkManDao linkManDao;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(LinkMan man) {
        linkManDao.save(man);
    }

    @Override
    public List<LinkMan> findAll() {
        return linkManDao.findAll();
    }

    @Override
    public List<LinkMan> findByDc(DetachedCriteria dc) {
        return linkManDao.findByDc(dc);
    }

    @Override
    public LinkMan findById(Integer lkmId) {
        return linkManDao.findById(lkmId);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void update(LinkMan linkMan) {
        linkManDao.update(linkMan);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delete(LinkMan linkMan) {
        linkManDao.delete(linkMan);
    }

    @Override
    public PageBean<LinkMan> findByDc(DetachedCriteria dc, Integer currPage, Integer pageSize) {

        int totalCount = linkManDao.findCount(dc);
        int totalPage = 0;
        if(totalCount % pageSize == 0){
            totalPage = totalCount / pageSize;
        } else {
            totalPage = totalCount / pageSize + 1;
        }
        List<LinkMan> list = linkManDao.findPage(dc, (currPage - 1) * pageSize, pageSize);

        PageBean pageBean = new PageBean();
        pageBean.setCurrPage(currPage);
        pageBean.setPageSize(pageSize);
        pageBean.setList(list);
        pageBean.setTotalCount(totalCount);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }
}
